# -*- coding: utf-8 -*-
'''
Created on Apr 17, 2012

@author: LONG HOANG GIANG
'''
import os
import cStringIO as StringIO
import simplejson as json
import mechanize
import datetime
import gzip
from lxml import etree
from urlparse import urljoin


def buildTree(url):
    html = mechanize.urlopen(url).read()
    tree = etree.parse(StringIO.StringIO(html), parser=etree.HTMLParser(encoding='utf-8'))
    return tree

def stringify(node):
    _s = etree.XPath('string()')
    if type(node).__name__ == 'list':
        if len(node)==0: return ''
        node = node[0]
    return _s(node)
    
def getChapter(url, name):
    print 'getChapter {0} : {1}'.format(name, url)
    tree = buildTree(url)
    data = {'chapter': name, 'images': []}
    for item in tree.xpath("//div[@class='center clear']/p/img"):
        src = item.get('src', '').strip()
        if src == '': continue
        src = urljoin(url, src)
        print src
        data['images'].append(src)
    return data

def getLisLink():
    
    url = 'http://truyentranh8.com/Tu_Dai_Danh_Bo/#doctruyen'
    tree = buildTree(url)
    if tree == None:
        print 'Error, can not build tree'
        return
    data = []
    for item in tree.xpath("//table[@id='tblChap']//tr[@class='chap']/td[1]/a"):
        name = stringify(item)
        link = item.get('href', '')
        if link == '': continue
        link = urljoin(url, link)
        item = getChapter(link, name)
        if len(item['images'])>0: data.append(item)
    
    f = gzip.open(output, "wb")
    f.write(json.dumps(data))
    f.close()
    return
    

if __name__ == '__main__':
    
    print 'start get truyentranh'
    
    output = 'tudaidanhbo.data'
    getLisLink()
    
    print 'finished at {0}'.format(datetime.datetime.now())
    os._exit(1)
    
    
